﻿using System;
using System.Collections.Generic;
using System.Linq;
using Nop.Core.Data;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Shops;
using Nop.Services.Customers;
using Nop.Services.Localization;

namespace Nop.Services.Shops
{
    public class ShopService:IShopService
    {
        private readonly ILocalizationService _localizationService;
        private readonly ICustomerService _customerService;
        private readonly IRepository<Shop> _shopRepository;
        private readonly IRepository<Customer> _customerRepository;

        public ShopService(ILocalizationService localizationService, ICustomerService customerService, IRepository<Shop> shopRepository, IRepository<Customer> customerRepository)
        {
            _localizationService = localizationService;
            _customerService = customerService;
            _shopRepository = shopRepository;
            _customerRepository = customerRepository;
        }

        public CreateShopAndModeratorResult CreateShopAndModerator(string shopName, int id)
        {
            var result = new CreateShopAndModeratorResult();


            if (String.IsNullOrWhiteSpace(shopName))
            {
                result.AddError(_localizationService.GetResource("Shop.ShopNameIsNullOrWhiteSpace"));
                return result;
            }

            if (id == 0)
            {

                result.AddError(_localizationService.GetResource("Shop.OwnerIdIsNullOrEmpty"));
                return result;
            }



            try
            {

                var owner = _customerRepository.GetById(id);

               

                if (owner == null)
                {
                    result.AddError(_localizationService.GetResource("Shop.OwnerNotExits"));
                    return result;
                }

                if (owner.Shop != null)
                {
                    result.AddError(_localizationService.GetResource("Shop.OwnerAlreadyHasShop"));
                    return result;
                }

                var shop = new Shop()
                               {
                                   ShopName = shopName,
                                   Owner = owner
                               };

                owner.Shop = shop;

                _customerRepository.Update(owner);
                

              //  _shopRepository.Insert(shop);
            }
            catch (Exception e)
            {
                result.AddError(e.ToString());
                return result;
            }

            return result;
        }


        public CreateDefaultShopResult CreateDefaultShopForDefaultUser(string defaultUserEmail)
        {


            var result = new CreateDefaultShopResult();
            if (String.IsNullOrWhiteSpace(defaultUserEmail))
            {
                result.AddError(_localizationService.GetResource("Shop.DefaultUserEmailIsNullOrWhiteSpace"));
                return result;
            }

            try
            {

                var customer = _customerService.GetCustomerByEmail(defaultUserEmail);
                var shopName = _localizationService.GetResource("Shop.DefaultShopName");
                var shop = new Shop()
                               {
                                   Owner = customer,
                                   ShopName = shopName,
                               };

                customer.Shop = shop;

                _shopRepository.Insert(shop);
            }
            catch (Exception e)
            {
                result.AddError(e.ToString());
                return result;
            }

            return result;
        }

        public IList<Shop> GetAllShops()
        {
            return _shopRepository.Table.ToList();
        }
    }
}